package com.vincent.controller.basic;

import com.github.cage.Cage;
import com.github.cage.image.Painter;
import com.vincent.common.enums.SessionEnum;
import com.vincent.common.utils.RandomCharUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 验证码图片生成Action
 */
@Controller
public class CodeImageController {
    /**
     * 生成验证码图片
     * @param response
     * @param session
     * @throws java.io.IOException
     */
    @RequestMapping(value = "/codeImg")
    public void getImg(HttpServletResponse response,HttpSession session) throws IOException {
        Painter painter = new Painter(120,30,null,null,null,null);
        Cage cage = new Cage(painter,null,null,null,null,null,null);
        String random = RandomCharUtil.getRandom(4);//生成随机字符串
        session.setAttribute(SessionEnum.AUTHCODE.getCode(),random);
        cage.draw(random,response.getOutputStream());
    }
}
